<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Banner.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Banner.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Banners.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_BannerController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelBanner;
    private $modelUser;

    /////////////////////
    // O V E R I D E S //
    /////////////////////  
    public function init() {
        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelBanner = new End_Models_Banners();
        $this->modelUser = new End_Models_Users();

        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }
    
    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    ////////////////////   
    public function addAction() {
        //Create form enter info for new Banner
        $form = new Application_Form_Banner();

        //Set label for the button
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');

        //Set client view page form  = $form
        $this->view->form = $form;
        $this->view->bodyCopy = "<p >Nhập các thông tin về banner vào form dưới đây:</p>";
        $created_date = date('Y-m-d H:i:s');
        //If can get client request 
        if ($this->getRequest()->isPost()) {
            //cancel button     
            $cancel = $this->getRequest()->getPost('cancel', '0');

            if ($cancel) {
                $this->_helper->redirector('index');
            }
            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $name = $form->getValue('name');
                $text = $form->getValue('text');
//                $num_clicks = $form->getValue('num_clicks');
//                $created_date = $form->getValue('created_date');
                $start_date = $form->getValue('start_date');
                $expired_date = $form->getValue('expired_date');
                $client_id = (int) $form->getValue('client_id');
                $code = $form->getValue('code');
                $click_url = $form->getValue('click_url');
//                $target = $form->getValue('target');
                $format = $form->getValue('format');
                $image_url = $form->getValue('image_url');
//                $ordering = (int)$form->getValue('ordering');
//                $mode = $form->getValue('mode');
//                $timeout = (int)$form->getValue('timeout');
                $status = $form->getValue('status');

                //Create new Page object from data
                $banner = new Banner(null, $name, $text, null, $created_date, $start_date, $expired_date, $client_id,
                                $code, $click_url, null, $format, $image_url, null, null, null, $status);

                //Use insert function from Banner model
                $this->modelBanner->_insert($banner);

                //Move to index page
                $this->_helper->redirector('index');
            } else {
                //Return form
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $banner_id = (int) $this->getRequest()->getPost('banner_id');
                $banners = new End_Models_Banners();
                $banners->_delete($banner_id);
            }
            $this->_helper->redirector('index');
        } else {
            $banner_id = $this->_getParam('banner_id', 0);
            $this->view->Banners = Utility_Converter::objectToArray($this->modelBanner->_find($banner_id));
        }
    }

    public function editAction() {
        $banner_id = $this->_getParam('banner_id');
        $form = new Application_Form_Banner();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $name = $form->getValue('name');
                $text = $form->getValue('text');
//                $num_clicks = $form->getValue('num_clicks');
//                $created_date = $form->getValue('created_date');
                $start_date = $form->getValue('start_date');
                $expired_date = $form->getValue('expired_date');
                $client_id = (int) $form->getValue('client_id');
                $code = $form->getValue('code');
                $click_url = $form->getValue('click_url');
//                $target = $form->getValue('target');
                $format = $form->getValue('format');
                $image_url = $form->getValue('image_url');
//                $ordering = (int)$form->getValue('ordering');
//                $mode = $form->getValue('mode');
//                $timeout = (int)$form->getValue('timeout');
                $status = $form->getValue('status');

                $banner = new Banner($banner_id, $name, $text, null, null, $start_date, $expired_date, $client_id,
                                $code, $click_url, null, $format, $image_url, null, null, null, $status); //dua thong tin vao doi tuong
                //tao doi tuong model voi cac phuong thuc xu li doi tuong o tren
                $this->modelBanner->_update($banner);        //xu li doi tuong voi phuong thuc cua model
                $this->_helper->redirector('index');    //chuyen den trang index
            } else {
                $form->populate($formData);
            }
        } else {
            $banner_id = $this->_getParam('banner_id', 0);
            if ($banner_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelBanner->_find($banner_id)));
            }
        }
    }

    public function indexAction() {
        $banner = $this->modelBanner->fetchAll();
        $this->view->Banner = $banner;
    }

}

